/*   Carina
 *   Copyright (C) 2009 2010 2011  Zdravko Velinov
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "ce-viewer.hh"
#include "ce-app.hh"

#ifndef _WIN32
#   include <X11/Xlib.h>
#   include <QGuiApplication>
#   include <qpa/qplatformnativeinterface.h>
#endif

CEViewer::CEViewer(QWidget* parent)
    :   QWidget(parent, 0),
        m_CursorX(0),
        m_CursorY(0),
        m_WImpl(CEApplication::getRenderingLibrary()->createWindowImpl())
{
    this->setAttribute(Qt::WA_PaintOnScreen);
    this->setAttribute(Qt::WA_NoSystemBackground);

    this->setFocusPolicy(Qt::ClickFocus);
    this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);

    WindowInfo wi;
#ifdef _WIN32
    if(parent)
		wi.setWindowId(reinterpret_cast<Carina::WindowId>(parent->winId()));
#elif defined(LINUX)
    QPlatformNativeInterface *native = QGuiApplication::platformNativeInterface();
    if(!native)
        THROW_EXCEPTION("application has failed to acquire native Qt5 interface");
    Display *display = static_cast<Display *>(native->nativeResourceForWindow("display", NULL));
    if(parent)
        wi.setWindowId(parent->winId()), wi.setDisplayId(display);
#else
#   error "Unsupported platform"
#endif

    m_WImpl->create(*CEApplication::getWindowManager(), 800, 600, 1, wi);
    m_Renderer = m_WImpl->getRenderer();

    this->create((WId)m_WImpl->getWindowId());

    startTimer(20);
}

CEViewer::~CEViewer()
{
    m_WImpl->destroy();
}

QSize CEViewer::sizeHint() const
{
    return QSize(200, 200);
}

void CEViewer::resizeEvent(QResizeEvent* event)
{
    m_WImpl->resize(event->size().width(), event->size().height());
    emit resized(event->size().width(), event->size().height());
}

void CEViewer::paintEvent(QPaintEvent* event)
{
    m_WImpl->getRenderer()->clearAll();
    emit rendering();
    m_WImpl->swapBuffers();
}

void CEViewer::timerEvent(QTimerEvent* event)
{
    update();
}

void CEViewer::keyPressEvent(QKeyEvent* evt)
{
    emit keyDown(evt->key());
}

void CEViewer::keyReleaseEvent(QKeyEvent* evt)
{
    emit keyUp(evt->key());
}

void CEViewer::mousePressEvent(QMouseEvent* evt)
{
    m_CursorX = evt->x();
    m_CursorY = evt->y();
    emit mousePressed(evt->button());
}

void CEViewer::mouseReleaseEvent(QMouseEvent* evt)
{
    emit mouseReleased(evt->button());
}

void CEViewer::mouseMoveEvent(QMouseEvent* evt)
{
    int xrel = evt->x() - m_CursorX,
        yrel = evt->y() - m_CursorY;
    m_CursorX = evt->x();
    m_CursorY = evt->y();
    emit mouseMoved(m_CursorX, m_CursorY, xrel, yrel);
}

